<!--
 * @Author: wangding
 * @Date: 2020-11-11 15:58:31
 * @LastEditTime: 2021-01-21 11:31:39
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \solaxcloud\src\views\device\sale\add.vue
-->
<template>
    <div class="buyPage-main-box-add">
        <el-dialog
                :title="$t('add')"
        :visible="newVisibleGroup"
        width="35%"
        @close="()=>{$store.commit('setNewVisibleGroup',false)}"
        :close-on-click-modal='false'
        >   
            <el-row :gutter="20">
                <el-col :span="19" :offset="2">
                    <el-form ref="form" :model="form" label-width="120px" >
                        <el-form-item v-if='userTypeSapn ==1?false:true' :label="$t('index.type')" prop="type" :rules="[{ required: true, message: $t('index_js_notNull')}]">
                            <el-select v-model="form.type" filterable :placeholder="$t('pleaseSelect')" clearable style="width:100%">
                                <el-option
                                    v-for="item in typeList"
                                    :key="item.id"
                                    :label="item.text"
                                    :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item :label="$t('upgradeGroup.newGroupName')" prop="name" :rules="[{ required: true, message: $t('index_js_notNull')}]">
                            <el-input v-model="form.name" style="width:100%"  :placeholder="$t('pleaseSelect')">
                            </el-input>
                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>
            <span slot="footer" class="dialog-footer">
                <el-button @click="()=>{$store.commit('setNewVisibleGroup',false)}">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="save('form')">{{$t('sure')}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    components: {
    },
    props: {
        addValueList:{
            type:Array,
            default:()=>{return []}
        },
        selectDataList:{
            type:Array,
            default:()=>{return []}
        },
        inverterType:{
            type:String,
            default:''
        },
        scope:{
            type:String,
            default:''
        },
        userTypeSapn:{
            
        }
    },
    data() {
        return {
            typeList:[{
                id:'0',
                text:"VPP"
            },{
                id:'1',
                text:"SAPN"
            }],
            form:{
                name:"",
                type:"",
            },
        };
    },
    computed: {
        newVisibleGroup(){
            return this.$store.state.sysStore.newVisibleGroup
        }
    },
    watch: {

    },
    created() {

    },
    mounted() {

    },
    methods: {
        
        async vppgroupAddEntity(){
            if(this.userTypeSapn == 1){
                this.form.type = 1
            }
            let res = await this.API.vppgroupAddEntity(this.form)
            if(!res.success){
                if(res.msg){
                    if(res.msg == 1){
                        this.comm.showMsg(false, this.$t('device-group-repeat'))
                    }else if(res.msg == 2){
                        this.comm.showMsg(false, this.$t('device-update-group-limit10'))
                    }else{
                       this.comm.showMsg(false, this.$t('index.control_fail'))
                    }
                }
            }else{
                this.comm.showMsg(true, this.$t('index.control_success'))
                this.$store.commit('setNewVisibleGroup',false)
                this.$emit('uploadData',true)
            }
        },
        save(formName){
            this.$refs[formName].validate((valid) => {
              if (valid) {
                  this.vppgroupAddEntity()
              } else {
                console.log('error submit!!');
                return false;
              }
            });
        },
    },
};
</script>

<style  lang="less" >
.buyPage-main-box-add{
    .el-form-item__content{
        display: flex;
    }
    .icon-fontsize{
        font-size: 25px;
        margin-left: 10px;
        display: block;
        width: 25px;
        height: 33px;
    }
}
</style>
